package com.rocksdata.mc.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ctz.commonframe.ui.TitleBarActivity;
import com.rocksdata.mc.MobileCampusApplication;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.UserInfoManager;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_modify_password)
public class ModifyPasswordActivity extends TitleBarActivity {
    @ViewInject(R.id.et_old_password)
    private EditText etOldPassword;
    @ViewInject(R.id.et_new_password)
    private EditText etNewPassword;
    @ViewInject(R.id.et_confirm_password)
    private EditText etConfirmPassword;

    private String oldPassword, newPassword, confirmPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleMiddleText("修改密码");

    }

    @Event(value = {R.id.iv_old_password_clear, R.id.iv_new_password_clear, R.id.iv_confirm_password_clear, R.id.tv_modify_password})
    private void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_old_password_clear:
                etOldPassword.setText("");
                break;
            case R.id.iv_new_password_clear:
                etNewPassword.setText("");
                break;
            case R.id.iv_confirm_password_clear:
                etConfirmPassword.setText("");
                break;
            case R.id.tv_modify_password:
                if(checkInput()){
                    modifyPassword();
                }
                break;
        }
    }

    private boolean checkInput(){
        oldPassword = etOldPassword.getText().toString().trim();
        newPassword = etNewPassword.getText().toString().trim();
        confirmPassword = etConfirmPassword.getText().toString().trim();
        if (TextUtils.isEmpty(oldPassword)) {
            showToast("请输入原密码");
            return false;
        } else if (TextUtils.isEmpty(newPassword)) {
            showToast("请输入新密码");
            return false;
        } else if (TextUtils.isEmpty(confirmPassword)) {
            showToast("请输入确认密码");
            return false;
        } else if (!newPassword.equals(confirmPassword)) {
            showToast("确认密码和新密码不同，请确认");
            return false;
        }
        return true;
    }

    private void modifyPassword() {
        showLoadingDialog();
        BusinessManager.getAccountManager().modifyPassword(oldPassword, newPassword, new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                if(isSuccess){
//                    UserInfoManager.setModifyPasswordFlag(true);
                    showToast("修改密码成功");
                    exit();
                    Intent intent = new Intent(MobileCampusApplication.getContext(), LoginActivity.class);
                    startActivity(intent);
                }else{
                    showToast("修改密码失败," + message);
                }
                dismissLoadingDialog();
            }
        });
    }
}
